package com.wgchao.identityrightsmanagement.application.service;

import com.wgchao.identityrightsmanagement.domain.organization.model.*;
import com.wgchao.identityrightsmanagement.domain.organization.repository.DepartmentOfficeRepository;
import com.wgchao.identityrightsmanagement.domain.organization.repository.DepartmentRepository;
import com.wgchao.identityrightsmanagement.domain.organization.repository.OfficeRepository;
import com.wgchao.identityrightsmanagement.domain.permission.model.AssignPermissionsToOfficeService;
import com.wgchao.identityrightsmanagement.domain.permission.model.OfficePermissionInformation;
import com.wgchao.identityrightsmanagement.domain.permission.repository.OfficePermissionInformationRepository;
import com.wgchao.identityrightsmanagement.domain.resource.model.FuncIdentify;
import com.wgchao.identityrightsmanagement.domain.resource.model.FunctionResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.MenuResources;
import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResources;
import com.wgchao.identityrightsmanagement.domain.resource.repository.FuncRepository;
import com.wgchao.identityrightsmanagement.domain.resource.repository.MenuRepository;
import com.wgchao.identityrightsmanagement.domain.resource.repository.ProjectRepository;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: PermissionApplicationService
 * @author: wgchao
 * @createTime: 2020/6/28 5:11 PM
 * @Description: TODO
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PermissionApplicationService {
    @Autowired
    private OfficeRepository officeRepository;
    @Autowired
    private DepartmentRepository departmentRepository;
    @Autowired
    private DepartmentOfficeRepository departmentOfficeRepository;
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private MenuRepository menuRepository;
    @Autowired
    private FuncRepository funcRepository;
    @Autowired
    private AssignPermissionsToOfficeService assignPermissionsToOfficeService;
    @Autowired
    private OfficePermissionInformationRepository officePermissionInformationRepository;


    public void assignPermissionsToOffice(String departmentCode, String officeCode, String proMark, List<PermissionInfo> permissionInfos, String operator){
        Office office = officeRepository.findById(officeCode);
        if(office == null){
            throw new ObjectNotExistException("职位不存在");
        }
        Department department = departmentRepository.findById(departmentCode);
        if(department == null){
            throw new ObjectNotExistException("部门不存在");
        }
        DepartmentOffice departmentOffice = departmentOfficeRepository.findByDepartmentAndOffice(departmentCode, officeCode);
        if(departmentOffice == null){
            throw new ObjectNotExistException("部门中不存在该职位");
        }
        ProjectResources projectResources = projectRepository.findById(proMark);
        if(projectResources == null){
            throw new ObjectNotExistException("项目不存在");
        }
        if(permissionInfos!=null && !permissionInfos.isEmpty()){
            for(PermissionInfo permissionInfo : permissionInfos){
                MenuResources menuResources = menuRepository.findById(permissionInfo.getMenuMark());
                if(menuResources == null){
                    throw new ObjectNotExistException(permissionInfo.getMenuMark()+"菜单资源不存在");
                }
                List<FunctionResources> functionResourcess = new ArrayList<>();
                if(permissionInfo.getFuncMarks()!=null && !permissionInfo.getFuncMarks().isEmpty()){
                    for(String funcMark : permissionInfo.getFuncMarks()){
                        FunctionResources functionResources = funcRepository.findById(new FuncIdentify(permissionInfo.getMenuMark(), funcMark));
                        if(functionResources == null){
                            throw new ObjectNotExistException(funcMark+"功能资源不存在");
                        }
                        functionResourcess.add(functionResources);
                    }
                }
                OfficePermissionInformation officePermissionInformation = assignPermissionsToOfficeService
                        .assign(office, department, departmentOffice, projectResources, menuResources, functionResourcess);
                officePermissionInformationRepository.add(officePermissionInformation, operator);
            }
        }

    }

    //部门删除-清除部门下职位权限
    @EventListener
    public void clearDepartmentPermissions(DepartmentRemovedEvent departmentRemovedEvent){
        officePermissionInformationRepository.deleteAllByDepartment(departmentRemovedEvent.getDepartmentCode(), departmentRemovedEvent.getOperator());
    }


    //职位删除-清除职位权限
    @EventListener
    public void clearDepartmentPermissions(OfficeRemovedEvent officeRemovedEvent){
        officePermissionInformationRepository.deleteAllByOffice(officeRemovedEvent.getOfficeCode(), officeRemovedEvent.getOperator());
    }

    //部门重新分配职位-清除无效权限
    @EventListener
    public void cleanUpIllegalCorrespondence(AssignedOfficesToDepartmentEvent assignedOfficesToDepartmentEvent){
        System.out.println("部门重新分配职位-清除无效权限");
        officePermissionInformationRepository.cleanUpIllegalCorrespondence(assignedOfficesToDepartmentEvent.getDepartmentCode(),
                assignedOfficesToDepartmentEvent.getOfficeCodes(), assignedOfficesToDepartmentEvent.getOperator());
    }

}
